package com.mijie.homi.ns.core.handler;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.homi.common.constant.TypeCode;
import com.mijie.homi.common.protocol.HomiProtocol;
import com.mijie.homi.ns.core.HomiServer;
import com.mijie.homi.ns.domain.Constants;
import com.mijie.homi.ns.service.SessionService;

@Service
public class HeartBeatHandler implements Handler {

	@Autowired
	private SessionService sessionService;
	@Autowired
	private HomiServer homiServer;
	
	@Override
	public Object handler(IoSession session, HomiProtocol protocol) {
		Integer userId = (Integer)session.getAttribute(Constants.KEY_USER_ID);
		if(userId==Integer.MIN_VALUE) return null;
		
		String token = (String) session.getAttribute(Constants.KEY_TOKEN);
		if(token!=null){
			sessionService.keepAliveSession(token);
			homiServer.markUsed(session);
		}
		return null;
	}
	
}
